import { connect } from 'umi';
import { ContractOppositeCountContext } from '../common';
import { Form, Spin, message } from 'antd';
import { useState } from 'react';
import { reportOppositeByStepService } from '../services';
import { OppositeSearchFormView } from './OppositeSearchFormView';
import { OppositeCountEchartsView } from './OppositeCountEchartsView';

const OppositeCountManageView: React.FC<ContractAllCountAPI.IManageType> = ({ curRes }) => {
  /** 样式根目录 */
  const styleFixCls = 'contract-report-';

  /** 统计步长 */
  const [countStepType, setCountStepType] = useState<string>('月');

  const [countSearchForm] = Form.useForm();

  /** 供应商数据 */
  const [oppositeSearchInfo, setOppositeSearchInfo] = useState<Record<string, any>>({});

  /** 获取数据回调 */
  const allCountCallback = (res: any) => {
    if (res && res?.msgFlag) {
    } else {
      message.error(res?.msg);
    }
  };

  /** 引入接口 */
  const { reportOppositeByStepRun, reportOppositeByStepInfo, reportOppositeByStepLoading } = reportOppositeByStepService(
    curRes,
    allCountCallback,
  );

  return (
    <ContractOppositeCountContext.Provider
      value={{
        styleFixCls,
        reportByStepRun: reportOppositeByStepRun,
        echartsDetailInfo: reportOppositeByStepInfo,
        stepTypeValue: countStepType,
        curRes,
      }}
    >
      <Spin spinning={reportOppositeByStepLoading}>
        <div className={`${styleFixCls}allCountDiv`}>
          <OppositeSearchFormView
            changeStepType={setCountStepType}
            curRes={curRes}
            setOppositeSearchInfo={setOppositeSearchInfo}
            countSearchForm={countSearchForm}
          />
          <OppositeCountEchartsView
            searchParams={countSearchForm?.getFieldsValue()}
            oppositeSearchInfo={oppositeSearchInfo}
            setOppositeSearchInfo={setOppositeSearchInfo}
          />
        </div>
      </Spin>
    </ContractOppositeCountContext.Provider>
  );
};
export default connect(({ baseLogin }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
}))(OppositeCountManageView);
